package com.imooc.common.thread;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Created by deng_wb on 2019/8/17
 */
public class FutureTest {

    public static void main(String[] args)throws Exception {

        FutureTask<Integer> task = new FutureTask<>(()->{
            ThreadUtil.sleep(500);
            return 1000;
        }); // new Callable () { Integer call(); }

        new Thread(task).start();

        System.out.println(task.get()); // 阻塞

        ExecutorService service = Executors.newFixedThreadPool(5);
        Future<Integer> f = service.submit(()->{
            ThreadUtil.sleep(500);
            return 1;
        });

        System.out.println(f.isDone());
        System.out.println(f.get());
        System.out.println(f.isDone());
        service.shutdown();

    }
}
